<?php
/**
* This is part of appEngine, a framework kit embedded in webappkit
* @link http://webappkit.net
* @package appEngine
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* appEngine framework central service
* @author J.Ducastel <jeremie@ducastel.name>
* @uses httpRequestBean
* @uses wakRequestDef
* @uses wakEvent
* @todo rewrite routing system. use YAML config and wakRoute class
* Use also a httpDigest object or array to store and share globals
*/
class wakEvtEngine extends wakService {
/*--------------------------------------------------------------------
PUBLIC INSTANCE METHODS
--------------------------------------------------------------------*/
	/**
	 * serves an http request
	 * @access public
	 * @param httpRequestBean $request if null, serves current http request
	 * @return bool
	 */
	function serveRequest($request=null) {
		// setting error level
		// error_reporting(E_ERROR | E_USER_ERROR | E_WARNING |E_USER_WARNING | E_PARSE);
		// get current request if undefined
		if (!$request)
			$request=httpRequestBean::getCurrent();
		// searching for applying named route
		if (!$route=$this->_searchRoute($request)) {
			// we're lost !
			trigger_error("route not found !");
		} else {
			// echo '<pre>'; print_r($route);
			return $route->go($request);
		}
		// print_r($route);
		// launch route sequence
		//return $this->_goRoute($route);
	}/**/
	

/*--------------------------------------------------------------------
PUBLIC STATIC METHODS
--------------------------------------------------------------------*/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/
/*--------------------------------------------------------------------
DEFAULT MEMBERS
--------------------------------------------------------------------*/
	/**
	* @var array configuration data (to be overriden by the INI)
	*/
	var $cfg=array(
		'engine'=>array(
			'auto_route_event'=>true
			,'auto_route_template'=>true
		),'routes'=>array()
	);/**/

	/**
	 * @var wakRouteSet
	 */
	var $routeSet;

	
	/**
	 * @var httpRequestBean currently processed http request
	 * /
	 var $request;/**/
	
	/**
	 * @var httpResponseBean http response being built
	 * /
	 var $response;/**/
	
	/**
	 * @var wakStorage events storage
	 */
	//var $evStorage;
/*--------------------------------------------------------------------
PROTECTED INSTANCE METHODS
--------------------------------------------------------------------*/
	/**
	 * intialization
	 * @todo adjusting templates storage 'lang' criteria
	 * upon request accepted-language and available languages
	 */
	function _init() {
		// loading routeset from kit
		$yml=$this->kit->getPath().'routeset.yml'; // echo $yml;
		$this->routeSet=&new wakRouteSet($yml);
		$this->routeSet->set('kit',$this->kit->getId());
		// retrieving templates storage
		if ($this->kit->hasStorage('templates'))
			$this->templates=&$this->kit->getStorage('templates');
		// echo '<pre>'; print_r($this->evStorage); echo '</pre>';
		
	}/**/

	/**
	 * searches for route applying to request
	 * @access protected
	 * @param httpRequestBean
	 * @return wakRoute
	 */
	function & _searchRoute($req) {
		return $this->routeSet->routeForRequest($req);
	}
}
?>